#ifndef __VI_H
#define __VI_H
#include <iostream>
#include <string>
#include <linux/videodev2.h>
#include <pthread.h>
#include "videoframe.h"
using namespace std;

#define CAPTURE_USE_USERPTR 0
#define VIDEO_WIDTH 1280
#define VIDEO_HEIGHT 720
#define __MAX_REQBUFS   5
class VI
{

    private:
        bool m_run = false;
        pthread_t m_thread;
    protected:
        int m_fd;
        struct v4l2_fmtdesc m_fmtavi;   //  查询摄像头支持的格式:本机摄像头支持 MJPG YUYV422
        struct v4l2_format m_fmt;       //  设置摄像头格式
        uint32_t m_v4l2reqbufcnt = __MAX_REQBUFS;   //  默认申请4个缓冲区
        struct v4l2_requestbuffers  m_reqbufs;      //  申请内核空间
        // uint8_t *m_mmptr[__MAX_CAPTURE_FRAME_] = {0};      //  保存映射后用户空间的首地址
    public:
        struct list_head free_capture_queue;
        struct list_head used_capture_queue;
        struct video_frame capture_frame[__MAX_CAPTURE_FRAME_];
        struct list_head v4l2_buffer_quenu;
        string m_dev = "/dev/video0";
        int m_width = VIDEO_WIDTH;
        int m_height = VIDEO_HEIGHT;
    private:
    protected:
        int opendev(string dev,uint8_t permission);
        int v4l2_queryfmt();
        int v4l2_setfmt();
        int v4l2_reqbufs();
        int v4l2_mmanreqbufs();
    public:
        int v4l2_startcapture();
        int v4l2_stopcapture();
        int vi_put_frame(struct video_frame* pframe);
        struct video_frame* vi_get_frame();
        int getfd();
        bool isrunning();
        VI();
        ~VI();
};
static void* vi_capture_mplane_handle_pthread(void *ptr);
static void* vi_capture_plane_handle_pthread(void *ptr);
#endif